home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-20 / rs0422.zip / LEVEL1 / RTIMER.AS < prev    next >
Text File  |  1989-11-14  |  4KB  |  166 lines

  1. *TITLE    RTIMER - 'Timer Subroutines for MULRPT'
  2.  
  3.     GLOBAL    SETTMR,TICK
  4.     GLOBAL    _COLD
  5.  
  6. ;  Routines use a single CTC channel to implement a multi-
  7. ;  channel timer capability.  The SETTMR subroutine is called
  8. ;  to set a timer.  Each "tick" of the CTC interrupt calls
  9. ;  the TICK subroutine, which counts down the timers.  When
  10. ;  a timer expires its associated subroutine is called.
  11.  
  12. ;  If running on a TNC 2 there is no CTC and the SYNC line
  13. ;  from the console is used, and we are called from I_EXT
  14.  
  15. *INCLUDE RCONFIG.LIB
  16.  
  17. ;  Timer data structure definition
  18.  
  19. CNT    EQU    0        ;Timer count, 2 bytes
  20. ARG    EQU    2        ;Service sub. calling arg, 2 bytes
  21. SVC    EQU    4        ;Address of service subroutine
  22. TDSLEN    EQU    6        ;Size of data structure
  23.  
  24. *Heading 'SETTMR - Timer Initialization Subroutine'
  25. ;  SETTMR - Set a timer channel.
  26. ;
  27. ;  ***  WARNING: Enter with interrupts disabled -- or else!
  28. ;
  29. ;    Entry parameters:
  30. ;        A =    Channel number.
  31. ;        BC =    Timer value in ticks.
  32. ;        DE =    Argument to be passed to the service
  33. ;            subroutine in DE when called.
  34. ;        HL =    Address of service subroutine.
  35. ;    Return parameters:
  36. ;        Registers AF, BC, DE, HL trashed.
  37.  
  38.     PSECT    text
  39. SETTMR:
  40.     PUSH    HL        ;Save during calculations
  41.     PUSH    BC
  42.  
  43.     LD    L,A        ;Make 16 bit channel number
  44.  
  45.     ld    a,h
  46.     or    a        ;is address of routine >8000h?
  47.     jp    p,1f        ;nope --> all ok
  48.     LD    A,80h
  49.     LD    (_COLD),A
  50.     JP    0
  51.  
  52. ;  Calculate data structure address for this channel
  53.  
  54. ;    LD    L,A        ;Make 16 bit channel number
  55. 1:    LD    H,0
  56.     ADD    HL,HL        ;Channel * 2
  57.     LD    C,L        ;Copy to BC
  58.     LD    B,H
  59.     ADD    HL,HL        ;Channel * 4
  60.     ADD    HL,BC        ;Channel * 6 (offset)
  61.     LD    BC,TIMDS    ;Get data structure base
  62.     ADD    HL,BC        ;Point to data structure
  63.  
  64. ;  Fill data structure.
  65.  
  66.     POP    BC        ;Get time count
  67.     LD    (HL),C        ;Store it
  68.     INC    HL
  69.     LD    (HL),B
  70.     INC    HL
  71.     LD    (HL),E        ;Store calling arg
  72.     INC    HL
  73.     LD    (HL),D
  74.     INC    HL
  75.     POP    DE        ;Get service address
  76.     LD    (HL),E        ;Store it
  77.     INC    HL
  78.     LD    (HL),D
  79.     RET
  80.  
  81. *HEADING 'TICK - CTC Interrupt Handler'
  82. ;  TICK - CTC interrupt service.  Counts down each timer
  83. ;  channel if active (i.e. count is non-zero).  When count
  84. ;  reaches zero, calls service subroutine.
  85.  
  86. TICK:
  87.     PUSH    AF        ;Save interrupted context
  88.     PUSH    BC
  89.     PUSH    DE
  90.     PUSH    HL
  91.     PUSH    IX
  92.     PUSH    IY
  93.  
  94. ;  Loop executes once for each timer channel. (one extra for L2/L3 100ms tick)
  95.  
  96.     LD    A,NUMCH+1    ;Number of timers (and channels)
  97.     LD    HL,TIMDS    ;Base of data structures
  98. TLOOP:    LD    (COUNT),A    ;Set loop count
  99.     LD    C,L        ;Copy data structure pointer...
  100.     LD    B,H        ;...to BC for safekeeping
  101.     LD    E,(HL)        ;Get time count
  102.     INC    HL
  103.     LD    D,(HL)
  104.     LD    A,D        ;Test for zero count (inactive)
  105.     OR    E
  106.     JR    Z,NXTTMR    ;Inactive, skip to next channel
  107.     DEC    DE        ;Count down one
  108.     LD    (HL),D        ;Update time count
  109.     DEC    HL
  110.     LD    (HL),E
  111.     LD    A,D        ;Is it zero now?
  112.     OR    E
  113.     JR    NZ,NXTTMR    ;No, don't service it
  114.     INC    HL        ;Point to calling arg
  115.     INC    HL
  116.     LD    E,(HL)        ;Get calling arg
  117.     INC    HL
  118.     LD    D,(HL)
  119.     INC    HL
  120.     LD    A,(HL)        ;Get service address
  121.     INC    HL
  122.     LD    H,(HL)
  123.     LD    L,A
  124.     PUSH    BC        ;Save data pointer
  125.     CALL    VECTOR        ;Call the service sub.
  126.     POP    BC        ;Restore data pointer
  127. NXTTMR:    LD    HL,TDSLEN    ;Offset to next data
  128.     ADD    HL,BC
  129.     LD    A,(COUNT)    ;Get loop count
  130.     DEC    A
  131.     JR    NZ,TLOOP    ;Continue
  132.  
  133. TMRXIT:    POP    IY        ;Restore interrupted context
  134.     POP    IX
  135.     POP    HL
  136.     POP    DE
  137.     POP    BC
  138.     POP    AF
  139.  
  140.     COND    CTC .eq. FALSE
  141.     RET            ;Back to I_EXT (intr svc)
  142.     ENDC
  143.  
  144.     COND    CTC .eq. TRUE
  145.     EI
  146.     RETI            ;Back to mainline
  147.     ENDC
  148.  
  149. VECTOR:    ld    a,h        ;insure we are jumping to ROM
  150.     or    a        ;set flags
  151.     jp    p,1f        ;nope --> all ok
  152.     LD    A,80h
  153.     LD    (_COLD),A
  154.     JP    0        ;death - trash the switch
  155.  
  156. 1:    JP    (HL)        ;Vector to subroutine
  157.  
  158.     PSECT    bss
  159. CNTR:    DEFS    1        ;Counter to adjust H/W intr to a 10 ms tick
  160. COUNT:    DEFS    1        ;Loop counter
  161. TIMDS:    DEFS    TDSLEN*(NUMCH+1) ;Note: this area is zeroed by
  162.                  ;the main program at reset
  163.  
  164. *HEADING 'Symbol Table'
  165.     END
  166.